import sys
from PyQt5.QtCore import QAbstractTableModel, Qt
import a
import pandas as pd
import os
import threading
import random
from PyQt5.QtWidgets import *


class pandasModel(QAbstractTableModel):

    def __init__(self, data):
        QAbstractTableModel.__init__(self)
        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parnet=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None


def scanning():
    file_type = True
    path = ui.lineEdit.text()

    if path == '':
        ui.label_7.setText('Error')
    elif (not ui.radioButton_2.isChecked()) and (not ui.radioButton.isChecked()) and (not ui.radioButton_3.isChecked()):
        ui.label_7.setText('Error')
    else:
        try:
            count_number = os.listdir(path)
            if ui.radioButton.isChecked():
                count_number = [i if i.split('.')[-1] == 'xls' else '' for i in count_number]
            elif ui.radioButton_2.isChecked():
                count_number = [i if i.split('.')[-1] == 'xlsx' else '' for i in count_number]
            elif ui.radioButton_3.isChecked():
                count_number = [i if i.split('.')[-1] == 'csv' else '' for i in count_number]
                file_type = False
            else:
                pass
            count_number = list(set(count_number))
            try:
                count_number.remove('')
            except:
                pass
            ui.label_7.setText(str(len(count_number)))
        except:
            ui.label_7.setText('Error')

        if len(count_number) != 0:
            select_table = random.choice(count_number)

            if file_type:
                print(pd.read_excel(path+'\\'+select_table, low_memory=False, nrows=1000))
            else:
                print(pd.read_csv(path+'\\'+select_table))
                data = pd.read_csv(path+'\\'+select_table, low_memory=False, nrows=1000)

                ui.tableView.setModel(pandasModel(data))
                ui.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
                ui.tableView.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)


def monitor():
    # 监听
    ui.pushButton_4.clicked.connect(scanning)


def show():
    # MainWindow展示
    MainWindow.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = a.Ui_MainWindow()
    ui.setupUi(MainWindow)

    p1 = threading.Thread(show())
    p2 = threading.Thread(monitor())

    p1.start()
    p2.start()

    sys.exit(app.exec_())
# G:\爬虫\微博\user_id
# MainWindow.setFixedSize(996, 687)
